FROM skyuoi/ourchat:alpine-base AS chef
WORKDIR /app

FROM chef AS builder

ARG SCCACHE_OSS_BUCKET=""
ARG SCCACHE_OSS_ENDPOINT=""
ARG SCCACHE_ENABLED="false"

ENV SCCACHE_OSS_BUCKET=${SCCACHE_OSS_BUCKET}
ENV SCCACHE_OSS_ENDPOINT=${SCCACHE_OSS_ENDPOINT}
ENV SCCACHE_OSS_NO_CREDENTIALS=1

COPY .cargo /app/.cargo

COPY server /app/server
COPY Cargo.toml /app/Cargo.toml
COPY Cargo.lock /app/Cargo.lock
COPY service /app/service

RUN if [ "$SCCACHE_ENABLED" = "true" ]; then \
        export RUSTC_WRAPPER=/usr/bin/sccache && \
        cargo build --release --bin server --features=official; \
    else \
        cargo build --release --bin server --features=official; \
    fi

FROM alpine:latest AS ourchat-server

COPY --from=builder /app/target/release/server /usr/local/bin/server
COPY docker/config /etc/ourchat

CMD ["server", "-c", "/etc/ourchat/ourchat.toml", "--server-info", "/etc/ourchat/server_info.json"]
